﻿using DingVision.Axis.Models;
using DingVision.Axis.Views;
using DingVision.Infrastructure.Events;
using DingVision.Infrastructure.Models;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DingVision.Axis.ViewModels
{
    public class AxisPageViewModel : BindableBase
    {
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;
        #region 方法绑定
        private DelegateCommand<object> selectAxisPageCommand;
        public DelegateCommand<object> SelectAxisPageCommand =>
            selectAxisPageCommand ?? (selectAxisPageCommand = new DelegateCommand<object>(ExecuteSelectAxisPageCommand));

        void ExecuteSelectAxisPageCommand(object obj)
        {
            var region = regionManager.Regions["AxisContentRegion"];
            foreach (var view in region.Views)
            {
                region.Deactivate(view);
            }
            switch (obj.ToString())
            {
                case "0":
                    {
                        var view = (JogPage)region.Views.Where(t => t.GetType() == typeof(JogPage)).FirstOrDefault();
                        region.Activate(view);
                    }
                    break;
                case "1":
                    {
                        var view = (PointPage)region.Views.Where(t => t.GetType() == typeof(PointPage)).FirstOrDefault();
                        region.Activate(view);
                    }
                    break;
                case "2":
                    {
                        var view = (IOPage)region.Views.Where(t => t.GetType() == typeof(IOPage)).FirstOrDefault();
                        region.Activate(view);
                    }
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 构造函数
        public AxisPageViewModel(IRegionManager _regionManager, IEventAggregator _eventAggregator)
        {
            regionManager = _regionManager;            
            eventAggregator = _eventAggregator;
            eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
            DmcCard.Instance.StatusUpdate += Instance_StatusUpdate;
        }
        #endregion
        #region 自定义函数
        private void Instance_StatusUpdate(object sender, string status)
        {
            eventAggregator.GetEvent<ShellReceiveEvent>().Publish(new ShellReceiveItem() { ModuleName = "AxisModule", Content = status });
        }

        private void ShellSendEventReceived(string obj)
        {
            switch (obj.ToString())
            {
                case "Loaded":
                    DmcCard.Instance.Init();
                    break;
                default:
                    break;
            }
        }
        #endregion


    }
    
}
